﻿/*
创建人：zhangrj
日期：2022-10-12
描述：工步批量报工页面js
*/
var page = {
    rootPath: getRootPath(), //网站根路径
    url: getRootPath() + "/Ashx/ContainerStepInfoWorkReportHandler.ashx", //后台处理url,
    commonUrl: getRootPath() + "/Ashx/CommonHandler.ashx", //通用方法后台路径
    //设备分页参数
    pageTotal: 0,
    pageSize: 20,
    pageNumber: 1,
    init: function () {
        page.bind();
    },
    //绑定事件
    bind: function () {
        customizedEasyuiPagination('pp'); //定制化分页
        //列表加载
        $("#gdContainerInfo").datagrid({
            onLoadSuccess: function (data) {
                $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
                    total: page.pageTotal,
                    pageSize: page.pageSize,
                    pageNumber: page.pageNumber
                });
                customizedEasyuiPagination('pp'); //定制化分页
            },
            columns: [[
                 { field: 'ck', title: '选择', checkbox: true },
                 { field: 'CONTAINERNAME', title: '批次', width: 200 },
                 { field: 'WORKFLOWSTEPNAME', title: '工序', width: 150 },
                 { field: 'STEPNO', title: '工步号', width: 100 },
                 { field: 'STEPINFONAME', title: '工步名', width: 120 },
                 { field: 'WIPQTY', title: '在制数', width: 60 },
                 { field: 'DISQTY', title: '派工数', width: 60 },
                 { field: 'CREATEDATE', title: '派工日期', width: 100,
                     formatter: function (value, row, index) {
                         if (!isNullOrWhiteSpaceStr(value)) {
                             return dayjs(value).format('YYYY-MM-DD');
                         }
                     }
                 },
                 { field: 'IsFillCheckPoint', title: '检测项录入', width: 100 },
                 { field: 'NOTES', title: '备注', width: 100, editor: "textbox" },
                 { field: 'CONTAINERSTEPDISPATCHID', hidden: true }
                ]],
            onDblClickCell: function (index, field, value) {
                $(this).datagrid('beginEdit', index);
                var ed = $(this).datagrid('getEditor', { index: index, field: ['NOTES'] });
                $(ed.target).focus();
                $(this).datagrid('checkRow', index);
            },
            onClickRow: function (index) {
                $(this).datagrid('acceptChanges');
            }
        });
        //列表分页事件
        $('#pp').pagination({
            onSelectPage: function (pageNumber, pageSize) {
                customizedEasyuiPagination('pp'); //定制化分页
                page.clearPage(false, true, true);
                page.pageSize = pageSize; page.pageNumber = pageNumber;
                page.search();
            }
        });
        $(document).ready(function () {
            //绑定分页go按钮
            $('#pageGo').on("click", function () {
                page.clearPage(false, true, true);
                var pageNumber = $(this).parents('tr').find('.pagination-num').val();
                page.pageNumber = pageNumber;
                page.search();
            });
        });
        //查询信息
        $("#btnSearch").on("click", function () {
            page.clearPage(false, true, true);
            page.pageNumber = 1;
            page.search();
        });
        //重置查询
        $("#btnReset").on("click", function () {
            page.clearPage(true, true, true);
        });
        //批量报工事件
        $("#btnBatchWorkReport").on("click", function () {
            page.clearPage(false, false, true);
            page.save();
        });
        //实测值录入
        $("#btnInputCheckPoint").on("click", function () {
            $("#gdContainerInfo").datagrid('acceptChanges');
            var row = $("#gdContainerInfo").datagrid('getChecked'); //选中的批次
            if (row.length === 0) {
                displayMessage("未选择工步", false);
                return;
            }
            if (row.length > 1) {
                displayMessage("只能选择一个报工数据", false);
                return;
            }
            page.post(page.url, { action: "SetInputCheckPoint", mainDt: JSON.stringify(row) }, true, function (res) {
                if (!res.Result) {
                    displayMessage(res.Message, false);
                    return;
                }
                page.openStepCheckItem();
            });
        });
        //批次流水卡打印查看
        $('#btnViewContainer').on("click", function () {
            clearMessage();
            page.viewContainerPrint();
        });
        //文档查看按钮
        $('#btnViewDocument').on("click", function () {
            clearMessage();
            page.viewDoc();
        });
    },
    //查询
    search: function () {
        var formData = {
            action: "GetStepInfoBatchList",
            pageSize: page.pageSize,
            pageIndex: page.pageNumber,
            orderName: $("#txtMfgOrderName").textbox("getValue"),
            productName: $("#txtProductName").textbox("getValue"),
            containerName: $("#txtContainerName").textbox("getValue"),
            combinateName: $("#txtCombinate").textbox("getValue"),
            opType: $("#txtOpType").combobox("getValue")
        };
        page.post(page.url, formData, true, function (res) {
            if (!res.Result) {
                displayMessage(res.Message, res.Result);
                return;
            }
            page.pageTotal = res.RowCount;
            $("#gdContainerInfo").datagrid('loadData', { total: res.RowCount, rows: res.DBTable });
        });
    },
    //报工
    save: function () {
        $("#gdContainerInfo").datagrid('acceptChanges');
        $('#btnBatchWorkReport').linkbutton('disable'); //禁用按钮
        var rows = $("#gdContainerInfo").datagrid('getChecked'); //选中的报工数据
        if (rows.length === 0) {
            displayMessage2("未选择报工数据", false, ["btnBatchWorkReport"]);
            return;
        }
        page.post(page.url, { action: "SaveStepInfoBatchWorkReportData", paramDt: JSON.stringify(rows) }, true, function (res) {
            if (res.Result) {
                page.search();
            }
            displayMessage2(res.Message, res.Result, ["btnBatchWorkReport"]);
        });
    },
    //检测项弹出页面
    openStepCheckItem: function () {
        //打开弹出页面
        var url = getRootPath() + "/forms/Custom/InputCheckItemWorkerPopupForm.aspx?v=" + new Date().getTime();
        window.open(url);
        //        url += "&parentDiv=dg";
        //        $('#dg').dialog({
        //            title: '实测值录入',
        //            width: 1100,
        //            height: 800,
        //            top: 10,
        //            closed: false,
        //            cache: false,
        //            modal: true,
        //            content: '<iframe src="' + url + '" width="100%" height="99%" frameborder="0" scrolling="auto"></iframe>',
        //            // poPupData: selected, //弹出页面的传参
        //            onClose: function () {
        //                var returnValue = window.returnValue;
        //                if (returnValue != null) {
        //                    if (returnValue.isSuccess == true) {

        //                    }
        //                }
        //            }
        //        });
    },
    //预览批次打印页面 
    viewContainerPrint: function () {
        var selected = $("#gdContainerInfo").datagrid("getChecked");
        if (selected.length == 0) {
            displayMessage("请选择批次", false);
            return;
        }
        page.post(page.commonUrl, { action: "viewPrintPage", isReBiao: "1", params: JSON.stringify(selected) }, true, function (result) {
            if (!result.Result) {
                displayMessage(result.Message, false);
                return;
            }
            var url = result.ReturnData + "?v=" + new Date().getTime();
            window.open(url);
        });
    },
    //文档查看
    viewDoc: function () {
        var selected = $("#gdContainerInfo").datagrid("getChecked");
        if (selected.length == 0) {
            displayMessage("请选择批次", false);
            return;
        }
        var url = page.rootPath + '/forms/Custom/' + "ProcessDocumentViewPopupForm.aspx?v=" + new Date().getTime();
        url += "&productId=" + selected[0].PRODUCTID + "&workflowId=" + selected[0].WORKFLOWID;
        $('#dg2').dialog({
            title: '文档查看',
            width: 730,
            height: 590,
            top: 10,
            closed: false,
            cache: false,
            modal: true,
            content: '<iframe src="' + url + '" width="100%" height="99%" frameborder="0" scrolling="auto"></iframe>',
            onClose: function () {

            }
        });
    },
    //清除页面（查询区域，列表区域，填写区域，是否清空消息）
    clearPage: function (query, list, msg) {
        if (query) {
            $("#txtMfgOrderName").textbox("setValue", ""); //订单号
            $("#txtProductName").textbox("setValue", ""); //件号
            $("#txtContainerName").textbox("setValue", ""); //流水卡
        }
        if (list) {
            clearEasyuiDataGrid("gdContainerInfo"); //清空列表
        }
        if (msg) {
            clearMessage();
        }
    },
    //post数据（路径，参数，是否异步，成功返回方法）
    post: function (url, param, async, callback) {
        $.ajax({
            type: "Post",
            url: url,
            data: param,
            cashe: false,
            async: async,
            dataType: "json",
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (data) {
                $.messager.progress('close');
                callback(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                displayMessage(XMLHttpRequest.responseText, false);
                $("body").find(".easyui-linkbutton").each(function () {
                    $(this).linkbutton('enable');
                });
            }
        });
    }
}
page.init();